
#include <SDL.h>
#include "effect.h"
#include "config.h"
#include "map.h"

extern SDL_Surface * screen;
extern SDL_Surface * tiles;

void LineEffectInit(Effect *e, int glyph, 
                    char startX, char startY, Dir d,
                    unsigned char n, int delay)
{
    e->glyph[0] = glyph;
    e->pos[0].x = startX;
    e->pos[0].y = startY;
    e->frame_num = 1;
    e->frame_delay = delay;

    int i;
    for (i = 1; i < n; ++i) {
        if (d == dir_up) {
            if (e->pos[i-1].y == 0) {
                break;
            }
            else {
                e->pos[i].x = e->pos[i-1].x;
                e->pos[i].y = e->pos[i-1].y - 1;
                e->frame_num ++;
            }
        }
        else if (d == dir_down) {
            if (e->pos[i-1].y == MAP_ROWS -1 ){
                break;
            }
            else {
                e->pos[i].x = e->pos[i-1].x;
                e->pos[i].y = e->pos[i-1].y + 1;
                e->frame_num ++;
            }
        }
        else if (d == dir_left) {
            if (e->pos[i-1].x == 0 ){
                break;
            }
            else {
                e->pos[i].x = e->pos[i-1].x - 1;
                e->pos[i].y = e->pos[i-1].y;
                e->frame_num ++;
            }
        }
        else if (d == dir_right) {
            if (e->pos[i-1].x == MAP_COLS -1 ){
                break;
            }
            else {
                e->pos[i].x = e->pos[i-1].x + 1;
                e->pos[i].y = e->pos[i-1].y;
                e->frame_num ++;
            }
        }
        else if (d == dir_upleft) {
            if (e->pos[i-1].x == 0 ||
                e->pos[i-1].y == 0 ){
                break;
            }
            else {
                e->pos[i].x = e->pos[i-1].x - 1;
                e->pos[i].y = e->pos[i-1].y - 1;
                e->frame_num ++;
            }
        }
        else if (d == dir_upright) {
            if (e->pos[i-1].x == MAP_COLS - 1 ||
                e->pos[i-1].y == 0 ){
                break;
            }
            else {
                e->pos[i].x = e->pos[i-1].x + 1;
                e->pos[i].y = e->pos[i-1].y - 1;
                e->frame_num ++;
            }
        }
        else if (d == dir_downleft) {
            if (e->pos[i-1].x == 0 ||
                e->pos[i-1].y == MAP_ROWS - 1 ){
                break;
            }
            else {
                e->pos[i].x = e->pos[i-1].x - 1;
                e->pos[i].y = e->pos[i-1].y + 1;
                e->frame_num ++;
            }
        }
        else if (d == dir_downright) {
            if (e->pos[i-1].x == MAP_COLS - 1 ||
                e->pos[i-1].y == MAP_ROWS - 1 ){
                break;
            }
            else {
                e->pos[i].x = e->pos[i-1].x + 1;
                e->pos[i].y = e->pos[i-1].y + 1;
                e->frame_num ++;
            }
        }

    }
    printf("spell anim frame num: %d\n", e->frame_num);
}

void LineEffectDraw(Effect *e)
{
    if (e->frame_num == 0) return;

    int i;
    SDL_Rect src_rect, dest_rect;
    src_rect.w = dest_rect.w = TILE_WIDTH;
    src_rect.h = dest_rect.h = TILE_HEIGHT;

    src_rect.x = e->glyph[0] % TILE_COLS * TILE_WIDTH;
    src_rect.y = e->glyph[0] / TILE_COLS * TILE_HEIGHT;

    for (i = 0; i < e->frame_num; i ++) {
        dest_rect.x = MAP_OFFSETX + e->pos[i].x * TILE_WIDTH;
        dest_rect.y = MAP_OFFSETY + e->pos[i].y * TILE_HEIGHT;
        SDL_BlitSurface(tiles, &src_rect, screen, &dest_rect);
        SDL_Flip(screen);
        SDL_Delay(e->frame_delay);
    }

    SDL_Delay(e->frame_delay);

    ClipRect r;
    GetClipRect(&r, e->pos[0], e->pos[e->frame_num - 1]);
    MapDraw(r);
    SDL_Flip(screen);
}

